import Koa from "koa";
import Router from "koa-router";
import bodyParser from "koa-bodyparser";

const ipPool = [
    '192.168.0.1',
    '192.168.0.2',
    '192.168.0.3',
    // 添加剩余的IP地址
];
// 生成剩余的IP地址
for (let i = 4; i <= 100; i++) {
    ipPool.push(`192.168.0.${i}`);
}
// 使用一个索引来跟踪当前已经使用的IP地址
let currentIndex = 0;

// 锁用于确保并发操作时的同步
// const lock = new Lock();

async function getUniqueIP() {
    // 加锁
    // await lock.acquire();

    // 获取当前索引对应的IP地址
    const currentIP = ipPool[currentIndex];

    // 更新索引，确保下次获取不同的IP地址
    currentIndex = (currentIndex + 1) % ipPool.length;

    // 释放锁
    // lock.release();

    return currentIP;
}
const app = new Koa();
const router = new Router();

router.get('/', async (ctx) => {
    ctx.body = await getUniqueIP();
});

app.use(bodyParser());
app.use(router.routes());


let port = 3000;
app.listen(port,()=>{
    console.log(`server port ${port} `);
});